package org.lw.ssm.service.impl;

import org.apache.logging.log4j.core.tools.picocli.CommandLine;
import org.lw.ssm.Entity.User;
import org.lw.ssm.mapper.UserMapper;
import org.lw.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    // 使用构造函数注入 UserMapper
    @Autowired
    public UserServiceImpl(UserMapper userMapper) {

        this.userMapper = userMapper;
    }

    // 插入用户
    public void addUser(User user) {

        userMapper.insertUser(user);
    }

    // 根据 ID 查询用户
    public User findUserById(int id) {

        return  userMapper.getUserById(id);
    }

    // 查询所有用户
    public List<User> findAllUsers() {
        return userMapper.getAllUsers();
    }

    // 更新用户
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    // 删除用户
    public int deleteUser(int id) {
        int i = userMapper.deleteUser(id);
        if(i==0)
            throw new RuntimeException();
        return i;
    }
}
//spring DATA　JPA
